/**   
* @Title: UserMapper.java
* @Package org.xaoyaoyao.cloud.hikari.mapper
* @Description: TODO
* @author xaoyaoyao
* @date Feb 14, 2019 9:26:53 AM
* @version V1.0   
*/
package org.xaoyaoyao.cloud.hikari.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;
import org.xaoyaoyao.cloud.hikari.model.User;
import org.xaoyaoyao.cloud.hikari.provider.UserSqlProvider;

/**
 * @ClassName: UserMapper
 * @Description: TODO
 * @author xaoyaoyao
 * @date Feb 14, 2019 9:26:53 AM
 * 
 */
@Mapper
@Component
public interface UserMapper {

	/**
	 * @Title: findAll
	 * @Description: 使用注解编写SQL
	 * @return List<User>
	 */
	@Select("SELECT * FROM t_user")
	List<User> findAll();

	@SelectProvider(type = UserSqlProvider.class, method = "listByUserName")
	List<User> listByUserName(String userName);

	/**
	 * @Title: findOne
	 * @Description: 延伸：上述两种方式都可以附加@Results注解来指定结果集的映射关系.(PS：如果符合下划线转驼峰的匹配项可以直接省略不写)
	 * @param id
	 * @return User
	 */
	@Select("SELECT * FROM t_user WHERE u_id = #{id}")
	@Results({ @Result(property = "uId", column = "u_id"), @Result(property = "userName", column = "user_name"), @Result(property = "password", column = "password"),
			@Result(property = "email", column = "email"), @Result(property = "remarks", column = "remarks"), @Result(property = "create_date", column = "createDate") })
	User findOne(Integer id);

	@Insert("INSERT INTO t_user(user_name, password, email, remarks, create_date) VALUES(#{userName}, #{password}, #{email}, #{remarks}, now())")
	@Options(useGeneratedKeys = true, keyProperty = "uId", keyColumn = "u_id") // 设置id自动增长，设置再对象里
	void insert(User user);

	@Update("UPDATE t_user SET user_name=#{userName}, password=#{password}, email=#{email}, remarks=#{remarks} WHERE id =#{id}")
	void update(User user);

	@Delete("DELETE FROM t_user WHERE u_id = #{id}")
	void delete(Integer id);

	/**
	 * 延伸：无论什么方式,如果涉及多个参数,则必须加上@Param注解,否则无法使用EL表达式获取参数。
	 */
	@Select("SELECT * FROM t_user WHERE user_name LIKE #{userName} AND password LIKE #{password}")
	User get(@Param("userName") String userName, @Param("password") String password);

	@SelectProvider(type = UserSqlProvider.class, method = "getBadUser")
	User getBadUser(@Param("userName") String username, @Param("password") String password);

}
